/**
 * 自动通知管理初始化
 */
var table1 = {
    id: "table3",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};
var table2 = {
    id: "table4",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};


var ids=new Array();
/**
 * 初始化表格的列
 */
table1.initColumn = function () {
    return [
        //{field: 'selectItem', radio: false},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: '单号', field: 'original_num', visible: true, align: 'center', valign: 'middle'},
        {title: '收货人', field: 'consignee', visible: true, align: 'center', valign: 'middle'},
        {title: '送货地址', field: 'receivingAddress', visible: true, align: 'center', valign: 'middle'},
        {title: '货物名称', field: 'goodsName', visible: true, align: 'center', valign: 'middle'},
        {title: '件数', field: 'goodsPiece', visible: true, align: 'center', valign: 'middle' },

        {title: '合计收款', field: '', visible: true, align: 'center', valign: 'middle',
            formatter : function(value,d,index){
                return parseFloat(d.extra_money)+parseFloat(d.tf)+parseFloat(d.hk)+parseFloat(d.cc)+parseFloat(d.byf);
            }
        },
        {title: '回单要求', field: 'receipt', visible: true, align: 'center', valign: 'middle' },
        {title: '入库时间', field: 'createtime', visible: true, align: 'center', valign: 'middle' }
    ];
};
Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

table1.callPhone = function () {

    var ids=new Array();
    var calls=new Array();
    var allTableData =$("#table4").bootstrapTable('getData');//获取表格的所有内容行
    if(allTableData.length<=0){
        Feng.error("请先选择要发送短信的订单");
        return ;
    }
    for( var i=0;i<allTableData.length;i++){
        ids.push(allTableData[i].id);
        if(allTableData[i].push_str!=''&&allTableData[i].push_str!=null){
            calls.push(allTableData[i].original_num);
        }
    }

    var str='是否确认语音通知?'+(calls.length>0?("其中"+calls.toString()+"已发送通知"):"");
    parent.layer.confirm(str, {
        btn: ['确定', '取消'],
        shade: false //不显示遮罩
    }, function () {
        var ajax = new $ax(Feng.ctxPath + "/tOrder/callPhone", function (data) {
            Feng.success("通知成功!");
            //aTable.refresh();
        }, function (data) {
            Feng.error("通知失败!" + data.responseJSON.message + "!");
        });
        ajax.set("ids", ids.join(","));
        ajax.start();
    });

};

table1.sendMsg = function () {
    var ids=new Array();
    var calls=new Array();
    var allTableData =$("#table4").bootstrapTable('getData');//获取表格的所有内容行
    if(allTableData.length<=0){
        Feng.error("请先选择要发送短信的订单");
        return ;
    }
    for( var i=0;i<allTableData.length;i++){
        ids.push(allTableData[i].id);
        if(allTableData[i].push_str!=''&&allTableData[i].push_str!=null){
            calls.push(allTableData[i].original_num);
        }
    }
        var str='是否确认短信通知?'+(calls.length>0?("其中"+calls.toString()+"已发送短信"):"");
        parent.layer.confirm(str, {
            btn: ['确定', '取消'],
            shade: false //不显示遮罩
        }, function () {
            var ajax = new $ax(Feng.ctxPath + "/tOrder/sendMsg", function (data) {
                Feng.success("通知成功!");
                //aTable.refresh();
            }, function (data) {
                Feng.error("通知失败!" + data.responseJSON.message + "!");
            });
            ajax.set("ids", ids.join(","));
            ajax.start();
        });


};

table1.batchOutage = function () {
    var ids=new Array();
    var allTableData =$("#table4").bootstrapTable('getData');//获取表格的所有内容行
    for( i=0;i<allTableData.length;i++)
    {
        if(allTableData[i].flow_status>2){
            Feng.error("不能选择已出库订单");
            return false;
        }else{
            ids.push(allTableData[i].id);
        }
    }

    if(ids.length>0){
        var index = layer.open({
            type: 2,
            title: '自提批量出库',
            area: ['1150px', '450px'], //宽高
            fix: false, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/tOrder/batchOutage?ids=' + ids.join(",")
        });
        layer.full(index);
        this.layerIndex = index;
    }else{
        Feng.error("请选择需要出库的订单");
    }
};


table1.search = function () {
    var queryData = {};
    queryData['name'] = $("#name").val();
    queryData['status'] = $("#status").val()+"";
    queryData['receipt_status'] = $("#receipt_status").val()+"";
    if ($('#is_out').is(":checked")) {
        queryData['is_out'] = 1;
    }else{
        queryData['is_out'] = 0;
    }
    table1.table.refresh({query: queryData});
};

function onDblClickRow(row)
{

    $("#table4").bootstrapTable('insertRow', {index:1, row:row});
    $("#table3").bootstrapTable('remove', {field: 'id', values: [row.id]});
    ids.push(row.id);

    var sum=0;
    var selected=$('#table4').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        sum+=(selected[i].ldf+selected[i].tf+selected[i].hk);
    }
    // $("#hj_div").show();
    // $('#hj').html(sum);
}

function removeAll()
{

$("#table4").bootstrapTable('removeAll');

}

function removeRow(row)
{
    $("#table4").bootstrapTable('remove', {field: 'id', values: [row.id]});
    $("#table3").bootstrapTable('insertRow', {index:1, row:row});
    ids.remove(row.id)
    var sum=0;
    var selected=$('#table4').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        sum+=(selected[i].ldf+selected[i].tf+selected[i].hk);
    }
    // $("#hj_div").show();
    // $('#hj').html(sum);

}


function loadData(data){
    var selected=$('#table4').bootstrapTable('getData', true);
    for(var i=0;i<selected.length;i++){
        for(var j=0;j<data.rows.length;j++){
            if(data.rows[j].id==selected[i].id){
                $("#table3").bootstrapTable('remove',  {field: 'id', values: [selected[i].id]});
            }
        }
    }

}



$(function () {
    var defaultColunms = table1.initColumn();
    var mTable = new BSTable(table1.id, "/tOrder/batchOutListData?flag=0", defaultColunms);
    mTable.setDblClickRow(onDblClickRow);
    mTable.setOnLoadSuccess(loadData);
    mTable.init();
    table1.table = mTable;

    var table = new BSTable(table2.id, "", defaultColunms);
    table.setDblClickRow(removeRow);
    table.init();
    table2.table = table;

    layui.use('layer', function () {

    })
    $('#receipt_status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"回单状态"
    });
    $('#status').selectpicker({
        style: 'btn btn-select',
        noneSelectedText:"出库状态"
    });

});

$(document).keyup(function(event) {
    if (event.keyCode == 13) {
        table1.search();
    }
});